<?php
/**
 * Created by PhpStorm.
 * User: qixiuxiu
 * Date: 2017/5/6
 * Time: 11:41
 */
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Person;
use AppBundle\Entity\Note;
use Symfony\Component\HttpFoundation\Response;

class PersonController extends Controller
{
    /**
     * @Route("/find", name="seek_search")
     */
    public function findAction(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $data = $request->request->all();
            //获得上传文件名称
            $file1 = $_FILES['profile_urls']['name'];
            $file2 = $_FILES['photo_url']['name'];
            //加存储路径
            $path = "D:\phpStudy\WWW\Symfony-demo\web\img\pic";
            //字符串链接
            $file1name =$path."\\".$file1;
            $file2name =$path."\\".$file2;

        if ( $data['family_name'] != '' || $data['checkbox'] != 'checked'){
        //实例化对象
        $person = new Person();
        //为person表中加记录
        $person->setProfileUrls($file1name);
        $person->setPhotoUrl($file2name);
        $person->setFamilyName($data['family_name']);
        $person->setFullName($data['full_name']);
        $person->setAlternateNames($data['alternate_names']);
        $person->setGivenName($data['given_name']);
        $person->setSex($data['sex']);
        $person->setDateOfBirth($data['date_of_birth']);
        $person->setAge($data['age']);
        $person->setHomeCountry($data['home_country']);
        $person->setHomeCity($data['home_city']);
        $person->setHomeStreet($data['home_street']);
        $person->setHomeNeighborhood($data['home_neighborhood']);
        $person->setHomeState($data['home_state']);
        $person->setHomePostalCode($data['home_postal_code']);
        $person->setDescription($data['description']);
        $person->setAuthorName($data['author_name']);
        $person->setAuthorEmail($data['author_email']);
        $person->setAuthorPhone($data['author_phone']);
        $time = time();
        $person->setEntryDate($time);
     
        //取出了Doctrine的 entity manager 对象，它负责处理数据库的持久化（译注：写入）和取出对象的过程
        $em = $this->getDoctrine()->getManager();
        //告诉doctrine你希望（最终）存储product对象（没有语句执行,persist($product) 调用，告诉Doctrine去 "管理" $product 对象。没有引发对数据库的请求。
        $em->persist($person);

        //真正执行语句(如，INSERT 查询）)
        $em->flush();
        }else{
                try{
                    //抛出异常
                    throw $this->createNotFoundException('请填写失踪人员姓名并承诺以上发布信息真实可靠！');
                }catch (Exception $e){

                }
            }
        }
        return $this->render('AppBundle:Default:find.html.twig'
//            ,array('person'=> $data)
);
    }
    /**
     * 案例
     * @Route("/case", name="case")
     */
    public function caseAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $case =$em->getRepository('AppBundle:Person')->findAllOrderByName();
        $count = count($case);
        // replace this example code with whatever you need
        return $this->render('AppBundle:Default:case.html.twig', array('case' => $case,'count'=>$count));
    }
    /**
     * 失踪人详细信息
     * @Route("/personinfo/{id}", name="personinfo")
     */
    public function personinfoAction($id)
    {
            $em = $this->getDoctrine()->getManager();
            $case =$em->getRepository('AppBundle:Person')->find($id);;
           
        // replace this example code with whatever you need
        return $this->render('AppBundle:Default:personinfo.html.twig',array('case'=>$case));
    }
}
